package com.example.milktea.controller;


import com.example.milktea.entity.Material;
import com.example.milktea.entity.Store;
import com.example.milktea.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    @RequestMapping("/insert")
    @ResponseBody
    public Material insert(HttpServletRequest request) {
            Material material = new Material();
            Store store = new Store();
            String material_id=request.getParameter("material_id");
            Integer m_id = Integer.valueOf(material_id);
            String material_name=request.getParameter("material_name");
            String supplement_phone=request.getParameter("supplement_phone");
            String supplement_price=request.getParameter("supplement_price");
            double price=Double.valueOf(supplement_price);

            String store_number = request.getParameter("store_number");
            Integer number = Integer.valueOf(store_number);
            store.setMaterial_id(m_id);
            store.setStoreNumber(number);

           material.setMaterial_id(m_id);
           material.setMaterial_name(material_name);
           material.setSupplement_phone(supplement_phone);
           material.setSupplement_price(price);
           material.setStore(store);
            System.out.println(material.getMaterial_id());
            int result = this.materialService.insert(material);
            System.out.print("insert result:"+result);
            return material;
        }

    @RequestMapping("/updateSupplementPhone")
    @ResponseBody
    public Material updateSupplementPhone(HttpServletRequest request) {
        Material material = new Material();
        String material_id=request.getParameter("material_id");
        Integer m_id = Integer.valueOf(material_id);
        String supplement_phone=request.getParameter("supplement_phone");
        material.setMaterial_id(m_id);
        material.setSupplement_phone(supplement_phone);

        int result = this.materialService.updateSupplementPhone(material);
        System.out.print("update result:"+result);
        return material;
    }

    @RequestMapping("/updateSupplementPrice")
    @ResponseBody
    public Material updateSupplementPrice(HttpServletRequest request) {
        Material material = new Material();
        String material_id=request.getParameter("material_id");
        Integer m_id = Integer.valueOf(material_id);
        String supplement_price=request.getParameter("supplement_price");
        Double price=Double.valueOf(supplement_price);
        material.setMaterial_id(m_id);
        material.setSupplement_price(price);

        int result = this.materialService.updateSupplementPrice(material);
        System.out.print("update result:"+result);
        return material;
    }


}
